<template>
	<view>
		
		<block v-if="loading">
			<empty :loading="true"></empty>
		</block>
		<block v-else>
			
			
			<checkbox-group @change="change">
			
			<block v-for="( a , index ) in list" :key="a.title">
				
				<view class="sectionTitle bb pd10 pl15 whiteBg bold main" :class="{mt10 : index > 0}">
					{{a.title}}
				</view>
				
				<navigator class="bb" :url="`/pages/daliyRecord/daliyRecordList?id=${id}&itemId=${item.id}&content=${item.content}&title=${a.title}`" v-for="( item , curIndex) in a.list" :key="curIndex" hover-class="op9" >
					<view class="fixAutoNoPd middle whiteBg pr10">
						<view class="whiteBg ptb10 pl15" :class="item.dued ? 'red' : 'grey'">
							<view @tap.stop="stop">
								<label class="flex lt">
									<checkbox :value="item.id+''" :checked="item.hasRecords"/>
									<view class="ml5">
										{{item.content}}
										<view class="tag redBg ml5" v-if="!item.isNormal">异常</view>
										<view class="fz12" v-if="item.lastTime">
											{{item.days}}维护（{{item.lastTime}}）
										</view>
										<view class="fz12 grey" v-else>
											暂无维护记录
										</view>
									</view>
								</label>
								
							</view>
						</view>
						<view class="grey fz12 w55 text-right ">
							{{item.duration}}天/次
							<text class="sIcon-arrowRight ml5"></text>
						</view>
					</view>
				</navigator>
				
				
			</block>
			
			</checkbox-group>
			
			<view class="pd5 gray"></view>
			
			<view class="pd whiteBg">
				<textarea class="bd pd10 rds3" v-model="remark" name="remark" style="height: 100px;" placeholder="请在此输入备注…"></textarea>
				
				<view class="father ptb">
					<view class="fixAutoNoPd  middle">
						<view class="w80">
							是否正常
						</view>
						<view class="item ">
							<view class="flex rt">
								<view>
									<switch :checked="isNormal" @change="isNormal=$event.detail.value" color="#3385ff"/>
								</view>
							</view>
						</view>
					</view>
				</view>
				
				<view @tap="confirm" class="btn rds23 block gradualBg" :class="{op5 : !ids } ">
					确认已检查
				</view>
			</view>
			
		</block>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				id : "",
				list : [] ,
				daliyRecords : {} ,
				refresh : false ,
				isNormal : true ,
				loading : true ,
				role : -1 ,
				remark : "" ,
				ids : []
			}
		},
		
		computed : {
			factors(){
				var contents = this.list.map(item=>{
					return item.list.filter(cur=>{
						return this.ids.findIndex(id => id == cur.id) > -1 ;
					}) ;
				}) ;
				return this.combine(...contents).map(item=>item.content).join("；") ;
			},
			days(){
				var contents = this.list.map(item=>{
					return item.list.filter(cur=>{
						return this.ids.findIndex(id => id == cur.id) > -1 ;
					}) ;
				}) ;
				var durations = this.combine(...contents).map(item=>item.duration) ;
				return durations.length == 0 ? 7 : Math.min.apply( Math , durations );
			}
		},
		
		onLoad(p) {
			this.id = p.id ;
			this.role = uni.getStorageSync("role");
			var recently = uni.getStorageSync("recently") || {} ;
			this.remark = recently[this.id];
		},
		
		onShow() {
			this.loadData();
		},
		
		onPullDownRefresh() {
			if (this.refresh) {
				return ;
			}
			uni.startPullDownRefresh({});
			this.refresh = true ;
			this.loadData();
		},
		
		methods: {
			
			stop:function(e){
				
			},
			
			combine : function(){
			    let arr = [].concat.apply([], arguments);  //没有去重复的新数组 
			    return Array.from(new Set(arr));
			},
			
			loadData:function(e){
				var data = {stationId:this.id} ;
				this.kit.get("/api/daliyItems/list",data,res => {
					this.list = res.list ;
					this.daliyRecords = res.daliyRecords ;
					if (this.daliyRecords) {
						this.isNormal = this.daliyRecords.isNormal ;
						this.remark = this.daliyRecords.remark ;
					}
					let ids = this.list.map(item=>{
						var records = item.list.filter(item=>item.hasRecords).map(item=>item.id) ;
						return records ;
					}) ;
					this.ids = this.combine(...ids) ;
					this.loading = false ;
					this.refresh = false ;
				});
			},
			
			change:function(e){
				this.ids = e.detail.value ;
			},
			
			confirm:function(e){
				if ( this.ids.length == 0) {
					uni.showToast({
						title: '至少选择一项检查项',
						icon : 'none'
					});
					return ;
				}
				var data = {
					stationId : this.id ,
					"r.stationId" : this.id ,
					"r.remark" : this.remark ,
					"r.isNormal" : this.isNormal ,
					total : this.ids.length ,
					factors : this.factors ,
					days : this.days 
				} ;
				for (var i = 0; i < this.ids.length; i++) {
					 var id = this.ids[i] ;
					 data[`x${i}.daliyItemsId`] = id ;
					 data[`x${i}.remark`] = this.remark ;
					 data[`x${i}.isNormal`] = this.isNormal ;
				}
				this.kit.get("/api/daliyRecord/batchSave",data,res => {
					uni.showToast({
						title: '操作成功',
						icon : 'success'
					});	
					var recently = uni.getStorageSync("recently") || {} ;
					recently[this.id] = this.remark ;
					uni.setStorageSync("recently" , recently);
					this.loading = true ;
					this.loadData();
				});
			},
		}
	}
</script>

<style>

</style>
